package com.tetris.project;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import engine.Score;

public class Tetris extends Activity {
	
	Music buttonSound;
	Bitmap bmp;
	RelativeLayout parentLayout;
	//LayoutParams spinParams;
	ArrayList<Score> profileList;
	final String PROFILES_FILE = "/data/data/com.tetris.project/profiles";
	int profileIndex;
	AlertDialog alertDialog;
	EditText profName;
	HandleScores myScores;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.d(Shr_Res.TAG, "Hello Tetris!");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        parentLayout = (RelativeLayout) findViewById(R.id.parent);
        myScores = new HandleScores(this);
        profileList = new ArrayList<Score>();
/***        profileList = readProfiles(PROFILES_FILE);***/
        setContentView(R.layout.main);
        profileList = myScores.readProfiles(profileList, PROFILES_FILE);
    }
    
    @Override
    protected void onStart() {
    	Log.d(Shr_Res.TAG, "Start Tetris.onStart");
    	super.onStart();      
    	//if(Shr_Res.music){Music.play(this, R.raw.main);}
    	// res/raw/main.mp3
    	buttonSound = new Music(this);
    	Shr_Res.wellcomeMusic = new Music(this);
    	Log.d(Shr_Res.TAG, "End Tetris.onStart");
    }
    
    // R.raw.main is main.mp3
    @Override
    protected void onResume() {
    	Log.d(Shr_Res.TAG, "Start Tetris.onResume");
    	super.onResume();
    	if(Shr_Res.music){Shr_Res.wellcomeMusic.play(R.raw.main);}
    	if(!Shr_Res.music){Shr_Res.wellcomeMusic.stop();}
    	// res/raw/main.mp3
    	Shr_Res.screen = true;
    	Log.d(Shr_Res.TAG, "End Tetris.onResume");
    }
    @Override
      protected void onPause() {
    	Log.d(Shr_Res.TAG, "Start Tetris.onPause");
    	super.onPause();
    	//Music.stop(this);
    	Log.d(Shr_Res.TAG, "End Tetris.onPause");
    }
    
    protected void onStop(){
    	Log.d(Shr_Res.TAG, "Start Tetris.onStop");
    	super.onStop();
    	Shr_Res.wellcomeMusic.stop();
    	Log.d(Shr_Res.TAG, "End Tetris.onStop");
    }
    
    protected void onDestroy(){
    	Log.d(Shr_Res.TAG, "Start Tetris.onDestroy");
    	super.onDestroy();
    	Log.d(Shr_Res.TAG, "End Tetris.onDestroy");
    }
    
    public void clickHandler(View view) {
    	Log.d(Shr_Res.TAG, "A Button pressed");
    	switch (view.getId()) {
    	case R.id.newButton:
    		Log.d(Shr_Res.TAG, "New Button pressed");
    		openNewGameDialog();
/***    		openSelectProfileDialog();***/
    		break;
    	case R.id.optionsButton:
    		Log.d(Shr_Res.TAG, "Options Button pressed");
    		Intent optionsIntent = new Intent(this, Options.class);
    		startActivity(optionsIntent);
    		break;
    	case R.id.exitButton:
    		Log.d(Shr_Res.TAG, "Exit Button pressed");
    		finish();
    		break;
    	/*case R.id.continueButton:
    		Log.d(Shr_Res.TAG, "Continue Button pressed");
    		break;*/
    	case R.id.aboutButton:
    		Log.d(Shr_Res.TAG, "About Button pressed");
			Intent aboutIntent = new Intent(this, About.class);
    		startActivity(aboutIntent);   		
			break;
    	case R.id.scoresButton:
    		Log.d(Shr_Res.TAG, "Scores Button pressed");
			Intent scoresIntent = new Intent(this, ShowScores.class);
    		startActivity(scoresIntent);
			break;
/***		case R.id.acceptProfile:
			Score sc = new Score();
			if ( profileIndex == 0 ) {
				sc.setName(profName.getText().toString());
				sc.setScore(0);
				profileList.add(sc);
//***				writeProfiles(sc, PROFILES_FILE);
				myScores.writeProfiles(profileList, PROFILES_FILE);
				
			}
			else {
				sc = profileList.get(profileIndex - 1);
			}
			alertDialog.dismiss();
			openNewGameDialog();
			break;
***/
		}
    }

/***    private ArrayList<Score> readProfiles(String fileName) {
    	ArrayList<Score> profileList = new ArrayList<Score>();
    	try {
    		DataInputStream dis = new DataInputStream(new FileInputStream(fileName));
			ObjectInputStream ois = new ObjectInputStream(dis);
			Object o = null;
			while ( (o = ois.readObject()) != null ) {
				profileList.add((Score) o);
			}
			ois.close();
		} catch (EOFException e) {
			Log.d("Read File", "EOFExcpetion");
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "StreamCorruptedException");
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "FileNotFoundException");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "IOException");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "ClassNotFoundException");
			e.printStackTrace();
		} 
		return profileList;
    }
    
    private void writeProfiles(Score sc, String fileName) {
    	 try {
    		DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName));
			ObjectOutputStream oos = new ObjectOutputStream(dos);
			for ( int i = 0; i < profileList.size(); i++) {
				oos.writeObject(profileList.get(i));
			}
			oos.close();
		} catch (EOFException e) {
			Log.d("Write File", "EOFExcpetion");
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.d("Write File", "FileNotFoundException");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("Write File", "IOException");
			e.printStackTrace();
		}
    }
***/
/***    private void openSelectProfileDialog() {
    	AlertDialog.Builder builder;
    	Spinner s1;
    	LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.profile_dialog, (ViewGroup) findViewById(R.id.layout_profile));
		builder = new AlertDialog.Builder(this);
		builder.setView(layout);
		alertDialog= builder.create();
		alertDialog.show();
		s1 = (Spinner) layout.findViewById(R.id.profileSpinner);
		profName = (EditText) layout.findViewById(R.id.profNameEdit); 
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter.add("Add new profile");
		for ( int i = 0; i < profileList.size(); i++) {
			adapter.add(profileList.get(i).getName());
		}
		s1.setAdapter(adapter);
		s1.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(
				AdapterView<?> parent, View view, int position, long id) {
				profileIndex = position;
				if ( position == 0) {
					profName.setVisibility(View.VISIBLE);
					}
				else {
					profName.setVisibility(View.GONE);
				}
			}
			public void onNothingSelected(AdapterView<?> parent) {
				//showToast("Spinner1: unselected");
				}
		});
    }
***/
    private void openNewGameDialog() {
    	new AlertDialog.Builder(this)
    		.setTitle(R.string.new_difficulty)
    		.setItems(R.array.difficulty,
    		new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialoginterface, int i) {
    				startGame(i);
    			}
    		})
    		.show();
    	}
    
    private void startGame(int i) {
    	Log.d(Shr_Res.TAG, "clicked on Level " + i);
    	Shr_Res.dificulty = i;
    	Intent newGameIntent = new Intent(this, Game.class);
		startActivity(newGameIntent);
		//overridePendingTransition(R.anim.fade, R.anim.fade);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.options:
	    	startActivity(new Intent(this, Options.class));
	    	return true;
    	// More items go here (if any) ...
    	}
    	return false;
	}

}
